#include "wijiio.h"
#include <QFile>
#include <QMessageBox>
#include <QDebug>
#include <QTextStream>
WijiIO::WijiIO(QObject *parent) {
}
QString WijiIO::read(QString filename) {
    QFile file(filename);
    QString contents;
    if (WijiIO::isValid(filename)) {
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        contents = file.readAll();
    } else {
       contents = "";
    }
    return contents;
}
bool WijiIO::write(QString filename,QString contents) {
    QFile file(filename);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        stream << contents << endl;
        file.close();
        return true;
    }
    return false;
}
bool WijiIO::isValid(QString filename) {

    bool result;
    QFile file(filename);
    if (file.exists()) {
        /*if (file.isReadable()) {
            result = true;
        } else {
            QMessageBox::critical(0,
                                  WijiIO::tr("Critical I/O Error"),
                                  WijiIO::tr("This file is not readable: ") + filename
                                  );
            result = false;
        }*/
        result = true;
    } else {
        QMessageBox::critical(0,
                              "Critical I/O Error",
                              QString("This file does not exist: ") + filename
                              );
        result = false;
    }
    return result;
}
